package pers.vic.system.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;

import pers.vic.base.bo.system.SysDict;
import pers.vic.base.controller.BaseConsoleController;
import pers.vic.base.pager.CommonLookup;
import pers.vic.base.pager.PageInfo;
import pers.vic.base.pager.Remind;
import pers.vic.base.vo.BaseResponse;
import pers.vic.system.system.service.SysDictService;

/**
 * 数据字典表
 * 
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictController extends BaseConsoleController {

	@Resource
	private SysDictService sysDictService;

	// 重定向到当前页面的page页
	private static final String REDIRECT_CURRENT_INDEX = "redirect:/system/dict/";

	// JSP页面的页面前缀路径
	private static final String JSP_PATH = "system/dict/";

	// 绑定一些公共数据
	@ModelAttribute
	private void bindSelect(Model model) {

	}
	/**
	 * 获取所有的子节点
	 * @return
	 */
	@RequestMapping(value="findAllChildDict")
	@ResponseBody
	public BaseResponse findAllChildDict(@RequestParam(defaultValue="0")Long code) {
		List<SysDict> data = sysDictService.findAllChildDict(code);
		return BaseResponse.success(data);
	}
	
	@RequestMapping(value = "/0", method = RequestMethod.GET)
	public String add(Long pcode, Model model) {
		System.out.println("pcode = " + pcode);
		model.addAttribute("pcode", pcode);
		return JSP_PATH + "form";
	}
	
	/*********************************************************************************/
	/** 列表页 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String page(Model model) {
		PageInfo<SysDict> pager = sysDictService.list(getLookup());
		model.addAttribute("pager", pager);
		return JSP_PATH + "list";
	}
	
	
	
	

	/** 查询 */
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public String search(CommonLookup lookup) {
		setLookup(lookup);
		return REDIRECT_CURRENT_INDEX;
	}

	/** 前往新增页面 */
	/*
	@RequestMapping(value = "/0", method = RequestMethod.GET)
	public String add(Model model) {
		return JSP_PATH + "form";
	}
	*/

	/** 前往修改页面 */
	@RequestMapping(value = "/{code}", method = RequestMethod.GET)
	public String editor(@PathVariable long code, Model model) {
		SysDict entity = sysDictService.findByCode(code);
		model.addAttribute("entity", entity);
		return JSP_PATH + "form";
	}
	
	@RequestMapping(value = "/findDict", method = RequestMethod.GET)
	@ResponseBody
	public BaseResponse editor(long code) {
		SysDict entity = sysDictService.findByCode(code);
		return BaseResponse.success(entity);
	}

	/** 保存 */
	@RequestMapping(value = "/{code}", method = RequestMethod.POST)
	public String save(@PathVariable long code, SysDict entity, RedirectAttributes attr) {
		entity.setCode(code);
		if (code == 0) {// to add
			sysDictService.insert(entity);
			attr.addFlashAttribute(Remind.success());
		} else if (code > 0) { // to update
			sysDictService.update(entity);
			attr.addFlashAttribute(Remind.success());
		} else {
			attr.addFlashAttribute(Remind.danger());
		}
		return REDIRECT_CURRENT_INDEX;
	}
	/**
	 * 异步保存
	 */
	@RequestMapping(value = "/asynSave/{code}", method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse asynSave(@PathVariable long code, SysDict entity, RedirectAttributes attr) {
		entity.setCode(code);
		System.out.println(JSONObject.toJSONString(entity));
		if (code == 0) {// to add
			sysDictService.insert(entity);
			attr.addFlashAttribute(Remind.success());
		} else if (code > 0) { // to update
			sysDictService.update(entity);
			attr.addFlashAttribute(Remind.success());
		} else {
			attr.addFlashAttribute(Remind.danger());
		}
		return BaseResponse.success();
	}

	/** 逻辑单个删除 */
	@RequestMapping(value = "/{code}/remove", method = RequestMethod.GET)
	public String delete(@PathVariable Long code, RedirectAttributes attributes) {
		sysDictService.mockDelete(code);
		attributes.addFlashAttribute(Remind.success());
		return REDIRECT_CURRENT_INDEX;
	}
	
	/**
	 * 异步删除
	 */
	@RequestMapping(value = "/asynRemove", method = RequestMethod.POST)
	@ResponseBody
	public BaseResponse asynRemove(Long code) {
		System.out.println("code = " + code);
		sysDictService.mockDelete(code);
		return BaseResponse.success();
	}

	/** 批量删除 */
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public String batchDelete(int[] ids, RedirectAttributes attributes) {
		sysDictService.delete(ids);
		attributes.addFlashAttribute(Remind.success());
		return REDIRECT_CURRENT_INDEX;
	}

}
